import wx
import copy

from DataTool import DataTool

class DiaTable4ChangeRate(wx.Dialog):
    material_type = ['光缆', '电缆', '塑料', '木材', '水泥', '其他', '设备', '设备(不安装)']
    collist = ['%s小计', '%s运杂费：费率%.2f%%', '%s运输保险费：小计*%.2f%%', 
                   '%s采购保管费：小计*%.2f%%', '%s采购代理服务费(直接金额)', '%s合计']
    colnamelist = ['运杂费费率', '运输保险费费率', '采购保管费费率', '采购代理服务费(直接金额)', '税金']
    ret = {'运杂费费率':'', '运输保险费费率':'', '采购保管费费率':'', '采购代理服务费(直接金额)':'', '税金':''}
    namelist = ['运杂费', '运保费', '采购及保管费']
    BtnNames = ['保存', '取消']
    BtnFuns = ['self.OnSave', 'self.OnCancel']

    def __init__(self, parent, title, *args, **kwargs):
        self.gridname = kwargs.get('name', '表四设备')
        self.data = kwargs.get('data')
        ret = []
        super().__init__(parent, -1, title)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        datasizer = wx.BoxSizer(wx.HORIZONTAL)
        btnsizer = wx.BoxSizer(wx.HORIZONTAL)

        self.InitUI(datasizer)
        self.InitBtn(btnsizer)

        mainsizer.Add(datasizer)
        mainsizer.Add(btnsizer, 0, flag=wx.ALL|wx.ALIGN_RIGHT, border=0)

        self.SetSizer(mainsizer)
        mainsizer.Fit(self)
        mainsizer.SetSizeHints(self)
    
    def InitUI(self, sizer):
        for index, row in enumerate(self.colnamelist):
            thissizer = wx.BoxSizer(wx.VERTICAL)
            StaticText = wx.StaticText(self, -1, label=row)
            thissizer.Add(StaticText, flag=wx.EXPAND|wx.ALL|wx.ALIGN_LEFT, border=5)
            if isinstance(self.data, dict):
                try:
                    value = self.data[row]
                except IndexError:
                    value = ''
            else:
                value = ''
            TextCtrl = wx.TextCtrl(self, -1, size=(50,-1), name=row+'changetextctrl', value=str(value))
            thissizer.Add(TextCtrl, flag=wx.EXPAND|wx.ALL, border=5)
            sizer.Add(thissizer)

    def InitBtn(self, sizer):
        for index, name in enumerate(self.BtnNames):
            btn = wx.Button(self, -1, label=name, name=name+'btn')
            self.Bind(wx.EVT_BUTTON, eval(self.BtnFuns[index]), btn)
            sizer.Add(btn, 0, flag=wx.ALL|wx.EXPAND, border=5)

    def OnSave(self, event):
        waringemptnames = ['运杂费费率', '运输保险费费率', '采购保管费费率', '采购代理服务费(直接金额)', '税金']
        waringnumnames = ['运杂费费率', '运输保险费费率', '采购保管费费率', '采购代理服务费(直接金额)', '税金']
        waringemptmessage = '数据不能为空！'
        waringnummessage = '填入数据必须为数字！'

        for index, name in enumerate(self.colnamelist):
            obj = self.FindWindowByName(name+'changetextctrl')
            text = obj.GetLineText(0)
            if name in waringemptnames and text == '':
                dia = wx.MessageDialog(self, '"' + name + '"' + waringemptmessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING)
                dia.ShowModal()
                dia.Destroy()
                return
            if name in waringnumnames and not DataTool().isnum(text):
                dia = wx.MessageDialog(self, '"' + name + '"' + waringnummessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING)
                dia.ShowModal()
                dia.Destroy()
                return
            self.ret[name] = text
        self.data = self.ret
        self.EndModal(retCode=1)

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

    def ShowModal(self):
        ret = super().ShowModal()
        if ret == 5101:
            return ret
        elif ret == 1:
            return self.data
        else:
            return 5101




if __name__ == "__main__":
    import os
    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(10, 10))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)

        def OnClick(self, event):
            data = {'运杂费费率':2, '运输保险费费率':3, '采购保管费费率':4, '采购代理服务费(直接金额)':5, '税金':6}
            dia = DiaTable4ChangeRate(self, 'haha', name='haha', data=data)
            modal = dia.ShowModal()
            print(modal)
            dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()